package com.gragra.evaluateNouns.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import com.gragra.evaluateNouns.EvaluateNounsWithMapping;
import com.gragra.parseOptions.CommandLineException;
import com.gragra.parseOptions.Parser;
public class TestEvaluateNouns
{
	/**
	 * 
	 */
	private EvaluateNounsWithMapping enm;
	/**
	 * 
	 */
	private BufferedReader br;
	@Before
	public void setUp() throws Exception
	{
		this.enm = new EvaluateNounsWithMapping();
		StringReader sr = new StringReader("The|D cat|NN is|NN on|PP the|D mat.|NN\nThe|D stain|NN is|V on|PP the|D carpet.|Noun\nThe|D Tom|PN is|V on|PP the|D table.|NN");
		br = new BufferedReader(sr);
	}
	@Test
	public void testGetFrameSize() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		assertEquals(3,enm.getFrameSize());
		enm.addOptions(pr);
		pr.parse(new String[] {"-rT","Noun","NN","-fS","5","8"});
		enm.evaluate(pr, br);
		assertEquals(5,enm.getFrameSize());
	}
	@Test
	public void testSetMicroAverage() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		enm.addOptions(pr);
		assertTrue(enm.isMicroAverage());
		pr.parse(new String[] {"-rT","Noun","NN","-fS","5","8","-mA"});
		enm.evaluate(pr, br);
		assertFalse(enm.isMicroAverage());
	}
	@Test
	public void testSetPostMarker() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		enm.addOptions(pr);
		assertEquals(enm.getPostMarker(),0);
		pr.parse(new String[] {"-rT","Noun","NN","-fS","5","8","-mA","-postM","4"});
		enm.evaluate(pr, br);
		assertEquals(4,enm.getPostMarker());
	}
	@Test
	public void testSetPreMarker() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		enm.addOptions(pr);
		assertEquals(enm.getPreMarker(),0);
		pr.parse(new String[] {"-rT","Noun","NN","-fS","5","8","-mA","-preM","4"});
		enm.evaluate(pr, br);
		assertEquals(4,enm.getPreMarker());
	}
	@Test
	public void testEvaluate() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		enm.addOptions(pr);
		pr.parse(new String[] {"-rT","Noun","NN","-fS","5","8","-preM","0","-its","1","-minF","2","-minW","3",
									"-kU","-fS","3","-incF","1","-incW","5","-postM","0","-pO","-mA","-scO","initialLog"});
		double[] res = enm.evaluate(pr, br);
		assertEquals(0.8333333333,res[0],0.00001);
		assertEquals(0.8888888888,res[1],0.00001);
		assertEquals(0.860215,res[2],0.00001);
	}
	@Test
	public void testWriteWords() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		enm.addNoRelevantOptions(pr);
		pr.parse(new String[] {"-fS","5","8","-preM","0","-its","2","-minF","2","-minW","6",
									"-kU","-fS","3","-incF","1","-incW","3","-postM","0","-pO","-mA","-scO","initialLog"});
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		enm.writeWords(pr, this.br, bw);
		bw.close();
		String[] parts = sw.toString().split("\n");
		Set<String> se = new HashSet<String>();
		for(String s : parts)
		{se.add(s.trim());}
		assertTrue(se.contains("mat"));
		assertFalse(se.contains("on"));
	}
	@Test
	public void testWriteFrames() throws CommandLineException, IOException
	{
		Parser pr = new Parser();
		enm.addNoRelevantOptions(pr);
		pr.parse(new String[] {"-fS","5","8","-preM","0","-its","1","-minF","2","-minW","3",
									"-kU","-fS","3","-incF","1","-incW","5","-postM","0","-pO","-mA","-scO","initialLog"});
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		enm.writeFrames(pr, this.br, bw);
		bw.close();
		String[] parts = sw.toString().split("\n");
		assertEquals("< the _ . >",parts[0].trim());
		assertEquals("< The _ is >",parts[1].trim());
	}
}